package com.itbour.web.action.user;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.itbour.com.page.Page;
import com.itbour.common.base.BaseAction;
import com.itbour.common.util.StringUtils;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.dto.user.MemberInfoDto;
import com.itbour.service.user.MemberInfoService;
import com.opensymphony.xwork2.ActionContext;

/**
 * 
 * @author LQ 2017 会员用户管理
 * 
 */
public class GetMemerInfoAction extends BaseAction {

	/**
	 * 全局 常用变量
	 */
	private static final long serialVersionUID = 1L;
	private Map<String, Object> mapResult = new HashMap<String, Object>();
	private MemberInfoService memberInfoService;//会员service
	private List<MemberInfoDto> listMem;//会员实体集合
	
	private String findMobile;//通过手机号查询用户
	private String findName;//通过昵称
	private String findId;//通过用户ID
	private String findState;// 会员状态
	private String findDateStart;// 开始时间
	private String findDateEnd;// 截止时间
	
	private MemberInfoDto mem;

	/**分页获取 会员信息 return mapResult;
	 */
	public String pageMemberInfo() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto) session.get("user");
		HttpServletRequest request = ServletActionContext.getRequest();
		if (user != null && user.getUser_id() != null) {
			Integer pageNo = Integer.parseInt(request.getParameter("page"));
			Integer pageSize = Integer.parseInt(request.getParameter("rows"));
			Map<String, Object> parm = new HashMap<String, Object>();
			if (StringUtils.isNotEmpty(findMobile)) {
				parm.put("mobile", findMobile);
			}
			if (StringUtils.isNotEmpty(findName)) {
				parm.put("nickname", findName);
			}
			if (StringUtils.isNotEmpty(findId)) {
				parm.put("user_id", findId);
			}
			if (StringUtils.isNotEmpty(findState)){
				parm.put("state",findState);
			}
			if (StringUtils.isNotEmpty(findDateStart)){
				parm.put("start_time",findDateStart);
			}
			if (StringUtils.isNotEmpty(findDateEnd)){
				parm.put("end_time",findDateEnd);
			}
			
			Page page = memberInfoService.getMemberInfo(parm, pageNo, pageSize);
			mapResult.put("total", page.getTotalCount());
			mapResult.put("rows", page.getResult());
		}
		return SUCCESS;
	}
	
	/**分页获取 一键秀会员信息 return mapResult;
	 */
	public String pageYjxMemberInfo() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto) session.get("user");
		HttpServletRequest request = ServletActionContext.getRequest();
		if (user != null && user.getUser_id() != null) {
			Integer pageNo = Integer.parseInt(request.getParameter("page"));
			Integer pageSize = Integer.parseInt(request.getParameter("rows"));
			Map<String, Object> parm = new HashMap<String, Object>();
			if (StringUtils.isNotEmpty(findMobile)) {
				parm.put("mobile", findMobile);
			}
			if (StringUtils.isNotEmpty(findName)) {
				parm.put("nickname", findName);
			}
			if (StringUtils.isNotEmpty(findId)) {
				parm.put("user_id", findId);
			}
			Page page = memberInfoService.getYjxMemberInfo(parm, pageNo, pageSize);
			if (mapResult!=null){
				mapResult.clear();
			}
			mapResult.put("total", page.getTotalCount());
			mapResult.put("rows", page.getResult());
		}
		return SUCCESS;
	}
	
	/**	分页获取用户信息
	 */
	public String pageUserRoleInfo(){
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		HttpServletRequest request=ServletActionContext.getRequest();
		if(user!=null && user.getUser_id()!=null){
			Integer pageNo = Integer.parseInt(request.getParameter("page"));
			Integer pageSize =Integer.parseInt(request.getParameter("rows"));
			Map<String,Object> parm=new HashMap<String, Object>();
			if(StringUtils.isNotEmpty(findMobile)){
				parm.put("mobile", findMobile);
			}
			if(StringUtils.isNotEmpty(findName)){
				parm.put("nickname", findName);
			}
			if (StringUtils.isNotEmpty(findId)) {
				parm.put("user_id", findId);
			}
			Page page=memberInfoService.getUserRoleInfo(parm, pageNo, pageSize);
			mapResult.put("total", page.getTotalCount());
			mapResult.put("rows",page.getResult());
		}
		return SUCCESS;
	}
	
	//获取一个会员信息
	public String getOneMemberInfo() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto) session.get("user");
		if (user != null && user.getUser_id() != null) {
			HttpServletRequest request = ServletActionContext.getRequest();
			Map<String, Object> parm =new HashMap<String, Object>();
			Integer mebId = Integer.parseInt(request.getParameter("id"));
			parm.put("member_id",mebId);
			listMem=memberInfoService.getOneMemberInfo(parm);
		}
		return SUCCESS;
	}
	
	//获取 单个一键秀 会员信息
	public String getOneYjxMemberInfo() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto) session.get("user");
		if (user != null && user.getUser_id() != null) {
			HttpServletRequest request = ServletActionContext.getRequest();
			Map<String, Object> parm =new HashMap<String, Object>();
			Integer mebId = Integer.parseInt(request.getParameter("id"));
			parm.put("member_id",mebId);
			if (listMem!=null){
				listMem.clear();
			}
			listMem=memberInfoService.getOneYjxMemberInfo(parm);
		}
		return SUCCESS;
	}

	// 添加会员
	public void addMemberInfo() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto) session.get("user");
		if (user != null && user.getUser_id() != null) {
			try {
				mem.setUpdate_by(user.getUser_name());
				Map<String, Object> parm=new HashMap<String, Object>();
				parm.put("user_id",mem.getUser_id());
				List<MemberInfoDto> list=new ArrayList<MemberInfoDto>();
				list=memberInfoService.getOneMemberInfo(parm);
				if(list.size()>0){
					this.writeJSON("exit");
				}else{
					memberInfoService.addMemberInfo(mem);
					this.writeJSON("ok");
				}
			} catch (Exception e) {
				this.writeJSON("no");
			}
		} else {
			this.writeJSON("null");
		}
	}
	// 修改一键生成会员
	public void updMemberInfo() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto) session.get("user");
		if (user != null && user.getUser_id() != null) {
			try {
				Map<String, Object> parm = new HashMap<String, Object>();
				parm.put("update_by",user.getUser_id());
				memberInfoService.updMemberInfo(mem);
				this.writeJSON("ok");
			} catch (Exception e) {
				this.writeJSON("no");
			}
		} else {
			this.writeJSON("null");
		}
	}
	// 删除会员
	public void delMemberInfo() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto) session.get("user");
		if (user != null && user.getUser_id() != null) {
			HttpServletRequest request=ServletActionContext.getRequest();
			try {
				Map<String, Object> parm = new HashMap<String, Object>();
				Integer mebId = Integer.parseInt(request.getParameter("ids"));
				parm.put("update_by",user.getNickname());
				parm.put("ids",mebId);
				memberInfoService.delMemberInfo(parm);
				this.writeJSON("ok");
			} catch (Exception e) {
				this.writeJSON("no");
			}
		} else {
			this.writeJSON("null");
		}
	}
	
	/**
	 * 添加一键秀会员
	 * @throws Exception
	 */
	public void addYjxMemberInfo() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto) session.get("user");
		if (user != null && user.getUser_id() != null) {
			try {
				mem.setUpdate_by(user.getUser_name());
				Map<String, Object> parm=new HashMap<String, Object>();
				parm.put("user_id",mem.getUser_id());
				List<MemberInfoDto> list=new ArrayList<MemberInfoDto>();
				list=memberInfoService.getOneYjxMemberInfo(parm);
				if(list.size()>0){
					this.writeJSON("exit");
				}else{
					memberInfoService.addYjxMemberInfo(mem);
					this.writeJSON("ok");
				}
			} catch (Exception e) {
				this.writeJSON("no");
			}
		} else {
			this.writeJSON("null");
		}
	}
	/**
	 * 修改一键秀会员
	 * @throws Exception
	 */
	public void updYjxMemberInfo() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto) session.get("user");
		if (user != null && user.getUser_id() != null) {
			try {
				Map<String, Object> parm = new HashMap<String, Object>();
				parm.put("update_by",user.getUser_id());
				memberInfoService.updYjxMemberInfo(mem);
				this.writeJSON("ok");
			} catch (Exception e) {
				this.writeJSON("no");
			}
		} else {
			this.writeJSON("null");
		}
	}
	/** 删除一键秀会员
	 */
	public void delYjxMemberInfo() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto) session.get("user");
		if (user != null && user.getUser_id() != null) {
			HttpServletRequest request=ServletActionContext.getRequest();
			try {
				Map<String, Object> parm = new HashMap<String, Object>();
				Integer mebId = Integer.parseInt(request.getParameter("ids"));
				parm.put("update_by",user.getNickname());
				parm.put("ids",mebId);
				memberInfoService.delYjxMemberInfo(parm);
				this.writeJSON("ok");
			} catch (Exception e) {
				this.writeJSON("no");
			}
		} else {
			this.writeJSON("null");
		}
	}
	/**	分页获取用户信息 用于添加会员
	 */
	public String pageGetYjxUserInfo(){
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		HttpServletRequest request=ServletActionContext.getRequest();
		if(user!=null && user.getUser_id()!=null){
			Integer pageNo = Integer.parseInt(request.getParameter("page"));
			Integer pageSize =Integer.parseInt(request.getParameter("rows"));
			Map<String,Object> parm=new HashMap<String, Object>();
			if(StringUtils.isNotEmpty(findMobile)){
				parm.put("mobile", findMobile);
			}
			if(StringUtils.isNotEmpty(findName)){
				parm.put("nickname", findName);
			}
			if (StringUtils.isNotEmpty(findId)) {
				parm.put("user_id", findId);
			}
			Page page=memberInfoService.getYjxUserInfo(parm, pageNo, pageSize);
			mapResult.put("total", page.getTotalCount());
			mapResult.put("rows",page.getResult());
		}
		return SUCCESS;
	}
	
	
	
	
	public Map<String, Object> getMapResult() {
		return mapResult;
	}

	public void setMapResult(Map<String, Object> mapResult) {
		this.mapResult = mapResult;
	}

	public MemberInfoService getMemberInfoService() {
		return memberInfoService;
	}

	public void setMemberInfoService(MemberInfoService memberInfoService) {
		this.memberInfoService = memberInfoService;
	}
	public String getFindMobile() {
		return findMobile;
	}
	public void setFindMobile(String findMobile) {
		this.findMobile = findMobile;
	}
	public String getFindName() {
		return findName;
	}
	public void setFindName(String findName) {
		this.findName = findName;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public MemberInfoDto getMem() {
		return mem;
	}
	public void setMem(MemberInfoDto mem) {
		this.mem = mem;
	}
	public List<MemberInfoDto> getListMem() {
		return listMem;
	}
	public void setListMem(List<MemberInfoDto> listMem) {
		this.listMem = listMem;
	}
	public String getFindId() {
		return findId;
	}
	public void setFindId(String findId) {
		this.findId = findId;
	}
	public String getFindState() {
		return findState;
	}
	public void setFindState(String findState) {
		this.findState = findState;
	}

	public String getFindDateStart() {
		return findDateStart;
	}

	public void setFindDateStart(String findDateStart) {
		this.findDateStart = findDateStart;
	}
	public String getFindDateEnd() {
		return findDateEnd;
	}
	public void setFindDateEnd(String findDateEnd) {
		this.findDateEnd = findDateEnd;
	}
	
}
